﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InductBase;
using SharpBolt.Graphics;
using System.ComponentModel;
using Newtonsoft.Json;

namespace SharpBolt.Graphics
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [JsonObject]
    public class GraphicInstance : IColorable, ITransformable, IDrawable
    {
        public Transformation Transformation { get; set; }
        public string Tag { get; set; }

        [JsonIgnore]
        public IGraphicSource Source { get; set; }

        public Color Color { get; set; }
        public virtual Vec2 Size { get; set; }
        public virtual Vec2 HotSpot { get; set; }
        public bool Hidden { get; set; }

        public virtual void Draw(Painter painter)
        {
            if (Source != null)
                if (!Hidden)
                    Source.Draw(painter, Transformation, Color);
        }

        public GraphicInstance()
        {
            HotSpot = Vec2.Null;
            Transformation = Transformation.Default;
            Color = Colors.White;
        }

        public virtual void Create() { }
        public virtual void Destroy() { }

        public virtual void Stretch(Rec area)
        {
            this.Transformation.Position = area.Location;
            this.Transformation.Scale = (area.Size / this.Size);
        }

        public virtual void ZoomInside(Rec area)
        {
            double minSide = Math.Min(area.Width, area.Height);
            Rec zoomArea = new Rec(0, 0, minSide, minSide);
            zoomArea.Center = area.Center;
            this.Transformation.Position = zoomArea.Location;
            this.Transformation.Scale = (zoomArea.Size / this.Size);
        }

        public virtual void ZoomOutside(Rec area)
        {
            double maxSide = Math.Max(area.Width, area.Height);
            Rec zoomArea = new Rec(0, 0, maxSide, maxSide);
            zoomArea.Center = area.Center;
            this.Transformation.Position = zoomArea.Location;
            this.Transformation.Scale = (zoomArea.Size / this.Size);
        }

        static public GraphicInstance EmptyInstance { get; private set; }
        static GraphicInstance()
        {
            EmptyInstance = new GraphicInstance();
            EmptyInstance.Hidden = true;
        }

        public override string ToString()
        {
            return "Src:" + Source;
        }
    }
}
